import React from 'react';
import { fetchPortrait } from '../../store/actions'
import { connect } from 'react-redux'
import FormEditSpecialist from '../../components/formEditSpecialist'
import _ from 'lodash'

class SpecialistEdit extends React.Component{
    constructor(props){
        super(props);
    }
    static contextTypes = {
        router: React.PropTypes.object
    };
    componentWillMount(){
        const _specialists = this.props.items,
          target = _.filter(_specialists,(_s)=>{
              return _s.account_id == this.props.params.id
          })[0];
        if(!target){
            this.context.router.push('/specialist-list')
        }else{
            const account_id = this.props.params.id;
            this.props.dispatch(fetchPortrait(account_id))
        }
        this.setState({
            target:target,
            portrait:this.props.portrait.url
        })
    }
    componentWillReceiveProps(nextProp){
        this.setState({
            portrait:nextProp.portrait.url
        })
    }
    render(){
        return (
            <FormEditSpecialist
              _target={this.state.target}
              portrait={this.state.portrait}
              user={this.props.user}
              dispatch={this.props.dispatch}
              id={this.props.params.id}
            />
        )
    }
}
export default SpecialistEdit = connect((state)=>{
    const {user, portrait} = state.rootReducer,
      {items} = state.rootReducer.specialist;
    return {user,items,portrait}
})(SpecialistEdit)